#include "PreCompile.hpp"
#include "InputFileSetTests.hpp"

namespace BuildNG
{
  InputFileSetTests::InputFileSetTests()
    : doc("TestFiles/InputFileSetTestsData.xml")
  {
  }

  Glib::RefPtr<InputFilesSet> InputFileSetTests::load_input_file_set(const Glib::ustring &name)
  {
    Xml::NodeSet node_set = doc.get_root_node()->
      find("/test-data/test[@name = '" + name + "']");
    assert(node_set.size() == 1);
    
    Xml::Element *elem = dynamic_cast<Xml::Element*>(node_set.front());
    assert(elem);

    Glib::RefPtr<InputFilesSet> ret = InputFilesSet::create();
    ret->add_group_from_xml(*elem);
    return ret;
  }

  void InputFileSetTests::test_input_files(
      Glib::RefPtr<InputFilesSet> set,
      const char **expected_file_names,
      int num_file_names)
  {
    assert(set->get_num_input_files_entries() == num_file_names);

    InputFilesSet::Iterator it = set->get_input_files_entry_iterator();
    int index = 0;
    while(it.move_next())
    {
      Glib::RefPtr<InputFiles> entry = it.get_entry();
      Glib::RefPtr<PhysicalInputFiles> file = Glib::RefPtr<PhysicalInputFiles>::cast_dynamic(entry);
      assert(file);

      assert(index < num_file_names);
      assert(file->get_path() == expected_file_names[index]);

      index++;
    }
  }

  void InputFileSetTests::test_simple_file_set()
  {
    Glib::RefPtr<InputFilesSet> the_set = load_input_file_set("simple-file-set");

    const char *filenames[] = 
    {
      "file1.cpp",
      "file2.cpp",
      "folder/file3.cpp",
    };
    test_input_files(the_set,filenames,sizeof(filenames) / sizeof(filenames[0]));
  }

  void InputFileSetTests::test_nested_groups()
  {
    Glib::RefPtr<InputFilesSet> the_set = load_input_file_set("nested-file-set");

    const char *filenames[] = 
    {
      "file1.cpp",
      "file2.cpp",
      "folder/file3.cpp",

      "image.bmp",

      "file1.hpp",
      "file2.hpp",
      "folder/file3.hpp",
    };
    test_input_files(the_set,filenames,sizeof(filenames) / sizeof(filenames[0]));
  }

  void InputFileSetTests::test_empty_group()
  {
    Glib::RefPtr<InputFilesSet> the_set = load_input_file_set("test-empty-group");
    const char *empty[] = {0};
    test_input_files(the_set,empty,0);
  }

  void InputFileSetTests::test_skip_empty_groups()
  {
    Glib::RefPtr<InputFilesSet> the_set = load_input_file_set("test-skip-empty-groups");

    const char *filenames[] = 
    {
      "a",
      "b",
    };
    test_input_files(the_set,filenames,sizeof(filenames) / sizeof(filenames[0]));
  }
}
